

function mergeTwoLists(l1: ListNode<number>, l2: ListNode<number>): ListNode<number> {
    let head: ListNode<number> = { val: -1, next: null };
    let tail = head;
    while(l1 !== null && l2 !== null) {
        if (l1.val <= l2.val) {
            tail.next = l1;
            l1 = l1.next;
        }
        else {
            tail.next = l2;
            l2 = l2.next;
        }
        tail = tail.next;
    }
    if (l1 !== null && l2 === null) tail.next = l1;
    else tail.next = l2;
    return head.next;
};

function toList(xs: number[]) {
    return xs.reduceRight((tail: ListNode<number>, x: number) => ({ val:x, next: tail }), null);
}

function toArray(head: ListNode<number>): number[] {
    var result: number[] = [];
    while(head !== null) {
        result.push(head.val);
        head = head.next;
    }
    return result;
}

var input: ListNode<number>[];
var xss: number[];

xss = toArray( mergeTwoLists( toList([2]), toList([1])))

